package com.demo.web.system;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.dto.CommonResult;
import com.demo.dto.DataGrid;
import com.demo.dto.Pager;
import com.demo.exception.ServiceLayerException;
import com.demo.model.system.Admin;
import com.demo.model.system.AdminExample;
import com.demo.model.system.Role;
import com.demo.service.system.AdminService;
import com.demo.service.system.RoleService;

@Controller
@RequestMapping(value="admin")
public class AdminController {

	@Autowired
	private AdminService adminService; 

	@Autowired
	private RoleService roleService; 	
	
	private Logger logger = Logger.getLogger(getClass());
	
	@RequestMapping(value="toListPage")
	public String toListPage(){
//		return "system/admin_list";
		return "system/admin_list_test";
	}

	@RequestMapping(value="getData_datagrid")
	@ResponseBody
	public Object getData_datagrid(Admin admin,Pager<Admin> pager){
		DataGrid<Admin> dataGrid = new DataGrid<Admin>();
		AdminExample example = new AdminExample();
		if (StringUtils.isNotEmpty(admin.getName())) {
			example.or().andNameLike(admin.getName());
		}
		try {
			pager = adminService.findByPage(example, pager);
			dataGrid.setRows(pager.getData());
			dataGrid.setTotal(adminService.getCount(example));
		} catch (Exception e) {
			logger.error("分页获取管理员异常", e);
		}
		return dataGrid;
	}
	
	@RequestMapping(value="getData_datagrid_test")
	@ResponseBody
	public Object getData_datagrid_test(Admin admin,Pager<Admin> pager){
		DataGrid<Admin> dataGrid = new DataGrid<Admin>();
		try {
			admin = adminService.getWithRolesById(admin.getId());
			dataGrid.setRows(Arrays.asList(admin));
			dataGrid.setTotal(1);
		} catch (Exception e) {
			logger.error("分页获取管理员异常", e);
		}
		return dataGrid;
	}
	
	@RequestMapping(value="getRoles")
	@ResponseBody
	public Object getRoles(Admin admin){
		CommonResult<List<Role>> result = new CommonResult<List<Role>>();
		try {
			List<Role> list = roleService.findByCondition(null);
			result.setData(list);
		} catch (Exception e) {
			if (e instanceof ServiceLayerException) {
				result.setBizException(((ServiceLayerException)e).getBizException());
			}
			logger.error("查询角色列表异常", e);
		}
		return result;
	}
	
	@RequestMapping(value="add")
	@ResponseBody
	public Object add(Admin admin){
		CommonResult<Boolean> result = new CommonResult<Boolean>();
		try {
			int count = adminService.add(admin);
			result.setData(count>0);
		} catch (Exception e) {
			if (e instanceof ServiceLayerException) {
				result.setBizException(((ServiceLayerException)e).getBizException());
			}
			logger.error("添加管理员异常", e);
		}
		return result;
	}

	@RequestMapping(value="getById")
	@ResponseBody
	public Object getById(Integer id){
		CommonResult<Admin> result = new CommonResult<Admin>();
		try {
			Admin admin = adminService.getById(id);
			result.setData(admin);
		} catch (Exception e) {
			if (e instanceof ServiceLayerException) {
				result.setBizException(((ServiceLayerException)e).getBizException());
			}
			logger.error("获取管理员异常", e);
		}
		return result;
	}
	
	@RequestMapping(value="modify")
	@ResponseBody
	public Object modify(Admin admin){
		CommonResult<Boolean> result = new CommonResult<Boolean>();
		try {
			int count = adminService.update(admin);
			result.setData(count>0);
		} catch (Exception e) {
			if (e instanceof ServiceLayerException) {
				result.setBizException(((ServiceLayerException)e).getBizException());
			}
			logger.error("修改管理员异常", e);
		}
		return result;
	}
	

	@RequestMapping(value="enable")
	@ResponseBody
	public Object enable(Integer id){
		CommonResult<Boolean> result = new CommonResult<Boolean>();
		try {
			int count = adminService.enable(Arrays.asList(id));
			result.setData(count>0);
		} catch (Exception e) {
			if (e instanceof ServiceLayerException) {
				result.setBizException(((ServiceLayerException)e).getBizException());
			}
			logger.error("启用管理员异常", e);
		}
		return result;
	}

	@RequestMapping(value="disable")
	@ResponseBody
	public Object disable(Integer id){
		CommonResult<Boolean> result = new CommonResult<Boolean>();
		try {
			int count = adminService.disable(Arrays.asList(id));
			result.setData(count>0);
		} catch (Exception e) {
			if (e instanceof ServiceLayerException) {
				result.setBizException(((ServiceLayerException)e).getBizException());
			}
			logger.error("停用管理员异常", e);
		}
		return result;
	}	
}
